import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../../core/routes/navigation_helper.dart';

/// 启动页面
class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> with TickerProviderStateMixin {
  // 主动画控制器
  late AnimationController _mainController;
  late AnimationController _logoController;
  late AnimationController _cornerController;

  // 主要动画
  late Animation<double> _backgroundFadeAnimation;
  late Animation<double> _logoScaleAnimation;
  late Animation<double> _logoFadeAnimation;
  late Animation<Offset> _logoSlideAnimation;
  late Animation<double> _logoBounceAnimation;

  // 四角装饰动画 - 依次淡入
  late Animation<double> _corner1FadeAnimation;
  late Animation<double> _corner2FadeAnimation;
  late Animation<double> _corner3FadeAnimation;
  late Animation<double> _corner4FadeAnimation;

  @override
  void initState() {
    super.initState();
    _initAnimations();
    _startAnimations();
    _navigateToHome();
  }

  void _initAnimations() {
    // 主动画控制器 - 3秒总时长
    _mainController = AnimationController(
      duration: const Duration(milliseconds: 3000),
      vsync: this,
    );

    // Logo动画控制器 - 2秒
    _logoController = AnimationController(
      duration: const Duration(milliseconds: 2000),
      vsync: this,
    );

    // 四角装饰动画控制器 - 2.5秒
    _cornerController = AnimationController(
      duration: const Duration(milliseconds: 2500),
      vsync: this,
    );

    // 背景淡入动画 (0-500ms)
    _backgroundFadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _mainController,
        curve: const Interval(0.0, 0.17, curve: Curves.easeIn),
      ),
    );

    // Logo缩放动画 (500-1500ms)
    _logoScaleAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _logoController,
        curve: const Interval(0.0, 0.5, curve: Curves.elasticOut),
      ),
    );

    // Logo淡入动画 (300-800ms)
    _logoFadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _logoController,
        curve: const Interval(0.15, 0.4, curve: Curves.easeIn),
      ),
    );

    // Logo从上方滑入动画 (0-800ms)
    _logoSlideAnimation = Tween<Offset>(
      begin: const Offset(0.0, -3.0), // 从屏幕上方开始
      end: Offset.zero,
    ).animate(
      CurvedAnimation(
        parent: _logoController,
        curve: const Interval(0.0, 0.4, curve: Curves.bounceOut),
      ),
    );

    // Logo跳动动画 (800-2000ms)
    _logoBounceAnimation = Tween<double>(begin: 1.0, end: 1.1).animate(
      CurvedAnimation(
        parent: _logoController,
        curve: const Interval(0.4, 1.0, curve: Curves.elasticInOut),
      ),
    );

    // 四角依次淡入动画
    _corner1FadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _cornerController,
        curve: const Interval(0.0, 0.3, curve: Curves.easeIn),
      ),
    );

    _corner2FadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _cornerController,
        curve: const Interval(0.2, 0.5, curve: Curves.easeIn),
      ),
    );

    _corner3FadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _cornerController,
        curve: const Interval(0.4, 0.7, curve: Curves.easeIn),
      ),
    );

    _corner4FadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _cornerController,
        curve: const Interval(0.6, 0.9, curve: Curves.easeIn),
      ),
    );
  }

  void _startAnimations() {
    // 延迟启动动画，创造更好的视觉效果
    Future.delayed(const Duration(milliseconds: 100), () {
      if (mounted) {
        _mainController.forward();
      }
    });

    Future.delayed(const Duration(milliseconds: 300), () {
      if (mounted) {
        _logoController.forward();
      }
    });

    Future.delayed(const Duration(milliseconds: 500), () {
      if (mounted) {
        _cornerController.forward();
      }
    });
  }

  void _navigateToHome() {
    Future.delayed(const Duration(milliseconds: 3500), () {
      if (mounted) {
        NavigationHelper.goToHome();
      }
    });
  }

  @override
  void dispose() {
    _mainController.dispose();
    _logoController.dispose();
    _cornerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedBuilder(
        animation: Listenable.merge([
          _mainController,
          _logoController,
          _cornerController,
        ]),
        builder: (context, child) {
          return Stack(
            children: [
              // 背景图片
              FadeTransition(
                opacity: _backgroundFadeAnimation,
                child: Container(
                  width: double.infinity,
                  height: double.infinity,
                  decoration: const BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/splash-bg.png'),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ),

              // 四个角落的装饰图片
              // 左上角 - splash1.png
              Positioned(
                top: -20.h,
                left: -10.w,
                child: FadeTransition(
                  opacity: _corner1FadeAnimation,
                  child: Image.asset(
                    'assets/images/splash1.png',
                    width: 118.26.w,
                    height: 343.11.h,
                    fit: BoxFit.cover,
                  ),
                ),
              ),

              // 右上角 - splash2.png
              Positioned(
                top: -20.h,
                right: -60.w,
                child: FadeTransition(
                  opacity: _corner2FadeAnimation,
                  child: Image.asset(
                    'assets/images/splash2.png',
                    width: 273.w,
                    height: 241.h,
                    fit: BoxFit.cover,
                  ),
                ),
              ),

              // 左下角 - splash3.png
              Positioned(
                bottom: -60.h,
                left: -30.w,
                child: FadeTransition(
                  opacity: _corner3FadeAnimation,
                  child: Image.asset(
                    'assets/images/splash3.png',
                    width: 247.77.w,
                    height: 247.77.h,
                    fit: BoxFit.cover,
                  ),
                ),
              ),

              // 右下角 - splash4.png
              Positioned(
                bottom: -50.h,
                right: -50.w,
                child: FadeTransition(
                  opacity: _corner4FadeAnimation,
                  child: Image.asset(
                    'assets/images/splash4.png',
                    width: 219.77.w,
                    height: 226.56.h,
                    fit: BoxFit.cover,
                  ),
                ),
              ),

              // 中央Logo
              Center(
                child: SlideTransition(
                  position: _logoSlideAnimation,
                  child: FadeTransition(
                    opacity: _logoFadeAnimation,
                    child: ScaleTransition(
                      scale: _logoScaleAnimation,
                      child: ScaleTransition(
                        scale: _logoBounceAnimation,
                        child: Container(
                          width: 104.w,
                          height: 104.w,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black.withValues(alpha: 0.3),
                                blurRadius: 20,
                                offset: const Offset(0, 10),
                                spreadRadius: 5,
                              ),
                            ],
                          ),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(20),
                            child: Image.asset(
                              'assets/images/logo.png',
                              width: 104.w,
                              height: 104.w,
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),

              Positioned(
                bottom: 50.h,
                left: 0,
                right: 0,
                child: FadeTransition(
                  opacity: _logoFadeAnimation,
                  child: Center(
                    child: SizedBox(
                      width: 30.w,
                      height: 30.w,
                      child: CircularProgressIndicator(
                        strokeWidth: 2,
                        valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
